<?php

namespace backend\models;

/**
 * @author  shuang
 * @date    2016-11-16 9:16:36
 * @version V1.0
 * @desc    
 */
use common\models\UserPayRecord;
use Yii;

class MY_UserPayRecord extends UserPayRecord {
    /*
     * 商户的交易明细
     */

    public static function getMerList() {
        $query = MY_UserPayRecord::find();
        $query->leftJoin(["userprofile" => MY_UserProfile::tableName()], "userprofile.userid=me_user_pay_record.userid");
        $query->select("me_user_pay_record.type,me_user_pay_record.class_type,me_user_pay_record.amount,me_user_pay_record.created_at,me_user_pay_record.description,userprofile.username");
        $query->andFilterWhere(["like", "userprofile.username", Yii::$app->request->post("search")]);
        $statusType = Yii::$app->request->post("statusType");
        $begin_at = Yii::$app->request->post("begin_at");
        $end_at = Yii::$app->request->post("end_at");
        if ($statusType > 0) {
            if ($statusType == 1) {
                $class_type = [1, 5, 8];
            }
            if ($statusType == 2) {
                $class_type = [4, 6, 7, 9];
            }
            if ($statusType == 3) {
                $class_type = [2, 3];
            }
            $query->andFilterWhere(["in", "class_type", $class_type]);
        }
        if ($begin_at) {
            $query->andFilterWhere([">=", "created_at", strtotime($begin_at)]);
        }
        if ($end_at) {
            $query->andFilterWhere(["<=", "created_at", strtotime($end_at)]);
        }
        $query->orderBy("me_user_pay_record.created_at desc");
        $countQuery = clone $query;
        $offset = Yii::$app->request->get("offset");
        $limit = Yii::$app->request->get("limit");
        $totalCount = $countQuery->count();
        $res = $query->offset($offset)->limit($limit)->asArray()->all();
        return ["total" => $totalCount, "data" => $res];
    }

    /*
     * 查询商户刷新、置顶、购买代理记录
     */

    public static function searchProfitList() {
        $query = MY_UserPayRecord::find();
        $begin_at = Yii::$app->request->post("begin_at");
        $end_at = Yii::$app->request->post("end_at");
        $class_type = Yii::$app->request->post("class_type") > 0 ? [Yii::$app->request->post("class_type")] : [7];
        $query->andFilterWhere(["in", "class_type", $class_type]);
        if ($begin_at) {
            $query->andFilterWhere([">=", "created_at", strtotime($begin_at)]);
        }
        if ($end_at) {
            $query->andFilterWhere(["<=", "created_at", strtotime($end_at)]);
        }
        $query->orderBy("created_at desc");
        $countQuery = clone $query;
        $totalCount = $countQuery->count();
        $res = $query->asArray()->all();
        return ["total" => $totalCount, "data" => $res];
    }

    public static $class_type_note = [
        7 => "任务置顶或刷新"
    ];
    public static $statusType = [
        1 => "充值",2=>"付款",3=>"退款"
    ];
}
